package top.humbleyuan.linkedlist;

import top.humbleyuan.datastructure.linkedlist.LinkNode;

/**
 * @Author HumbleYuan
 * @Date 2020/5/18 22:12
 * @Des 翻转链表 II
 */
public class LeetCode_92 {
    public static void main(String[] args) {
        /**
         * 数据准备
         */
        LinkNode head = new LinkNode(1);
        int[] vals = {4,3,6,7,8};
        head = LinkNode.formLink(head,vals);

        LinkNode root = new LinkNode(0);
        root.next = head;

        int m = 1,n = 3;
        LinkNode temp = head;
        LinkNode back = null ;
        LinkNode preTemp = root;
        LinkNode pre = null;
        LinkNode preNext = null;

        int i = 1;
        while(i <= n) {
            if(i >= m) {
                if(i == m) {
                    pre = preTemp;
                    preTemp = temp;
                    back = temp.next;
                    preNext = preTemp;
                    temp = back;
                    i++;
                    continue;
                }
                back = temp.next;
                temp.next = preTemp;
                preTemp = temp;
                temp = back;
                i++;
                continue;
            }
            preTemp = temp;
            temp = temp.next;
            i++;
        }

        preNext.next = back;
        pre.next = preTemp;

        LinkNode.headToString(root.next);
    }
}
